/*
 *    Copyright 2010 The maven-migrate-plugin Team
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */
package com.googlecode.mvnmigrate;

import java.io.File;
import java.util.List;
import java.util.Locale;
import java.util.ResourceBundle;

import org.apache.ibatis.migration.Change;
import org.apache.maven.doxia.sink.Sink;
import org.apache.maven.doxia.siterenderer.Renderer;
import org.apache.maven.project.MavenProject;
import org.apache.maven.reporting.AbstractMavenReport;
import org.apache.maven.reporting.MavenReportException;

/**
 * Extends {@link AbstractMavenReport}.
 * Class to generate a maven report.
 *
 * @version $Id: StatusCommandReportMojo.java 132 2010-07-10 17:23:54Z marco.speranza79 $
 * @goal status-report
 */
public class StatusCommandReportMojo extends AbstractMavenReport {

    /**
     * The Maven project to analyze.
     *
     * @parameter expression="${project}"
     * @required
     * @readonly
     */
    private MavenProject project;

    /**
     * Target folder.
     *
     * @parameter expression="${project.build.directory}"
     * @readonly
     */
    private File outputDirectory;

    /**
     *
     *
     * @component
     * @required
     * @readonly
     */
    private Renderer siteRenderer;

    /**
     * Location of migrate repository.
     *
     * @parameter expression="${repository}"
     * @required
     */
    protected File repository;

    /**
     * Environment to configure. Default environment is 'development'.
     *
     * @parameter expression="${environment}" default-value="development"
     */
    protected String environment;

    /**
     * Forces script to continue even if SQL errors are encountered.
     *
     * @parameter  expression="${force}" default-value="false"
     */
    protected boolean force;

    /**
     * {@inheritDoc}
     */
    @Override
    protected void executeReport(Locale locale) throws MavenReportException {
        // Step 0: Checking pom availability
        if ("pom".equals(this.project.getPackaging())) {
            if (this.getLog().isInfoEnabled()) {
                this.getLog().info("Skipping pom project");
            }
            return;
        }

        if (this.outputDirectory == null || !this.outputDirectory.exists()) {
            if (this.getLog().isInfoEnabled()) {
                this.getLog().info( "Skipping project with no Target directory" );
            }
            return;
        }

        // Step 1: Analyse the project
        List<Change> analysis = null;

        MigrationStatusCommand analyzer = new MigrationStatusCommand(repository, environment, force);
        try {
            analysis = analyzer.getMergedStatus();
        } catch (Exception e) {
            throw new MavenReportException("Cannot analyze migration status", e);
        }

        // Step 2: Create sink and bundle
        Sink sink = getSink();
        ResourceBundle bundle = getBundle( locale );

        // Step 3: Generate the report
        MigrationStatusReportView view = new MigrationStatusReportView();
        view.generateReport( analysis, sink, bundle );
    }

    /**
     * {@inheritDoc}
     */
    @Override
    protected String getOutputDirectory() {
        if (this.getLog().isInfoEnabled()) {
            this.getLog().info( outputDirectory.toString() );
        }
        return this.outputDirectory.toString();
    }

    /**
     * {@inheritDoc}
     */
    @Override
    protected MavenProject getProject() {
        return this.project;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    protected Renderer getSiteRenderer() {
        return this.siteRenderer;
    }

    /**
     * Return the output name of the report.
     */
    public String getOutputName() {
        return "migration-status-analysis";
    }

    /**
     * Retunr the name of the report.
     */
    public String getName(Locale locale) {
        return getBundle(locale).getString("migration.status.report.name");
    }

    /**
     * Retunr the description of the report.
     */
    public String getDescription(Locale locale) {
        return getBundle( locale ).getString("migration.status.report.description");
    }

    /**
     * The current locale.
     *
     * @param locale
     */
    protected ResourceBundle getBundle(Locale locale) {
        return ResourceBundle.getBundle("migration-report", locale, this.getClass().getClassLoader());
    }

}
